package com.yunchao.app

import android.app.Application
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.multidex.MultiDex
import com.tencent.mmkv.MMKV
import com.yunchao.app.data.dataModule
import com.yunchao.app.presentation.mainPresentationModule
import com.yunchao.feature.base.baseModule
import com.yunchao.feature.base.domain.utils.FileLoggingTree
import com.yunchao.feature.business.featureBusinessModules
import com.yunchao.feature.lock.featureLockModules
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.GlobalContext
import timber.log.Timber

class HFEDApplication : Application() {

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        MMKV.initialize(this)
        initKoin()
        initTimber()

        // 调用方式
        sendSettingBroadcast(this, "vkey2_disabled", "true")
        sendSettingBroadcast(this, "vkey3_disabled", "true")
        sendSettingBroadcast(this, "drop_down", "true")
    }

    private fun initKoin() {
        GlobalContext.startKoin {
            androidLogger()
            androidContext(this@HFEDApplication)
            modules(appModule)
            modules(baseModule)
            modules(featureLockModules)
            modules(mainPresentationModule)
            modules(dataModule)
            modules(featureBusinessModules)
        }
    }

    private fun initTimber() {
        Timber.plant(if (BuildConfig.DEBUG) Timber.DebugTree() else FileLoggingTree(this))
    }


    private fun sendSettingBroadcast(context: Context, key: String, value: String) {
        Intent(ACTION_SETTINGS_CHANGED).apply {
            component = ComponentName(PACKAGE_NAME, RECEIVER_COMPONENT)
            putExtra("key", key)
            putExtra("value", value)
        }.also { context.sendBroadcast(it) }
    }

    companion object {
        private const val ACTION_SETTINGS_CHANGED = "com.seuic.featuresettings.SETTINGS_CHANGED"
        private const val RECEIVER_COMPONENT = "com.seuic.featuresettings.SettingsReceiver"
        private const val PACKAGE_NAME = "com.seuic.featuresettings"
    }
}